#!/bin/sh

echo "###############################################################"
echo "### NUPP应用所涉及到的第三方中间件启动脚本 "
echo "### 如果是执行了安装脚本，则无需再执行该脚本，该脚本主要应用在服务器重启后，有些第三方软件没有自动重启的场景"
echo "###############################################################"

APPPATH=$(cd `dirname $0`;pwd)

start_redis(){	
  pid=`ps ux | grep redis-server |  grep -v "grep" | awk '{print $2}'`
  if [ -n "$pid" ] ; then
    echo "redis Server has running. No need to restart..."
    return 0
  fi
  echo "preparing to start redis..."

  # 执行启动redis命令
  export PATH=$PATH:/usr/local/redis
  startRedis.sh
  redis-cli -v

  if [ $? == 0 ]; then
    echo "redis server start success..."
  fi
}

start_minio(){	
  pid=`ps ux | grep minio | grep server | grep -v "grep" | awk '{print $2}'`
  if [ -n "$pid" ] ; then
    echo "minio Server has running. No need to restart..."
    return 0
  fi
  echo "preparing to start minio..."
  export MINIO_ACCESS_KEY=admin
  export MINIO_SECRET_KEY=12345678
  export MINIO_ADDRESS=:9000
  export MINIO_ROOT_USER=LJ7wjcOnGM00eDptlDNQ
  export MINIO_ROOT_PASSWORD=yX+M1Q5iaNJKIlQIwqtnv95cC0V76PuWRwWL9FiuUDU

  # 执行启动minio命令
  nohup minio server /usr/local/minio/data >/usr/local/minio/minio.log 2>&1 &

  if [ $? == 0 ]; then
    echo "minio server start success..."
  fi
}

start_elasticsearch(){	
  pid=`ps -ef | grep elasticsearch | grep java | grep -v "grep" | awk '{print $2}'`
  if [ -n "$pid" ] ; then
    echo "elasticsearch Server has running. No need to restart..."
    return 0
  fi
  echo "preparing to start elasticsearch..."

  # 执行启动elasticsearch命令
  elasticsearchPack=elasticsearch-7.17.18
  su - elasticsearch <<-EOF
  cd /home/elasticsearch/$elasticsearchPack/bin
  nohup sh elasticsearch &
  exit
EOF

  if [ $? == 0 ]; then
    echo "elasticsearch server start success..."
  fi
}

start_zookeeper(){	
  pid=`ps -ef | grep zookeeper | grep java | grep -v "grep" | awk '{print $2}'`
  if [ -n "$pid" ] ; then
    echo "zookeeper Server has running. No need to restart..."
    return 0
  fi
  echo "preparing to start zookeeper ..."

  # 执行启动zookeeper命令
  zookeeperPack=apache-zookeeper-3.9.2-bin
  su - zookeeper <<-EOF
  cd /home/zookeeper
  cd $zookeeperPack/bin
  nohup sh zkServer.sh start &
  exit
EOF

  if [ $? == 0 ]; then
    echo "zookeeper server start success..."
  fi
}

start_rabbitmq(){	
  rabbitmq_user=nupp_test
  echo ">>> CREATE NEW rabbitmq user"
  rabbitmq-plugins enable rabbitmq_management
  rabbitmqctl add_user $rabbitmq_user $rabbitmq_user
  rabbitmqctl set_user_tags $rabbitmq_user administrator
  rabbitmqctl set_permissions -p / $rabbitmq_user ".*" ".*" ".*"
}

#### MAIN ####
if [ $# == 0 ]; then
  # 启动所有程序
  start_redis
  start_minio
  start_elasticsearch
  start_zookeeper
  start_rabbitmq
elif [ $# == 1 ]; then
  case $1 in 
    "redis")
      start_redis
      ;;
    "minio")
      start_minio
      ;;	  
    "elasticsearch")
      start_elasticsearch
      ;;	  
    "zookeeper")
      start_zookeeper
      ;;
    "rabbitmq")
      start_rabbitmq
      ;;
    *)
      echo "invalid parameter [$1]..."
      ;;	  
  esac
fi



